<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/js/jquery-3.5.0.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/components.css">
    <link rel="stylesheet" href="/css/login.css">
    <title>本期秒杀商品</title>
</head>
<body>
<h3 class="h3 heading">本期秒杀商品</h3>
<div>
    <table class="table table-striped table-dark">
        <thead>
        <tr>
            <th scope="col">商品名称</th>
            <th scope="col">商品描述</th>
            <th scope="col">图片</th>
            <th scope="col">价格</th>
            <th scope="col">剩余库存</th>
        </tr>
        </thead>
        <tbody id="itemList">
        </tbody>
    </table>

</div>

</body>
<script>
    var item_list = [];
    $(function () {
        $.ajax({
                type: "GET",
                url: "/item/list",
                success: function (data) {
                    if(data.status==='success'){
                        item_list = data.data;
                        render();
                    }else {
                        alert("获取商品列表失败："+data.data.errMsg)
                    }
                },
                error: function (data) {
                    alert("获取秒杀列表失败：" + data.responseText)
                }
            }
        )
    })

    function render() {
        for (var i = 0; i < item_list.length; i++) {
            var trStr = '<tr onclick=getDetail('+item_list[i].id+')><td>' + item_list[i].title + '</td>' +
                '<td>' + item_list[i].description + '</td>' +
                '<td><img width="200px" src=' + item_list[i].imgUrl + '></td>' +
                '<td>' + item_list[i].price + '</td>' +
                '<td>' + item_list[i].stock + '</td></tr>'
            // console.log(trStr);
            $("#itemList").append($(trStr));
        }
    }
    function getDetail(item_id) {
        window.location.href = "/html/item_detail.html?id="+item_id
    }
</script>
</html>